package com.zhang.req.page;

import com.zhang.req.bean.PageView;
import com.zhang.second.utils.UserBehavior;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.Random;

/**
 * @title: 计算pv 解决数据倾斜问题
 * @author: zhang
 * @date: 2022/2/8 20:47
 */
public class PageViewCountPlus {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env
                .readTextFile("/Users/apple/IdeaProjects/flink_1.13/src/main/resources/UserBehavior.csv")
                .map(new MapFunction<String, UserBehavior>() {
                    @Override
                    public UserBehavior map(String value) throws Exception {
                        String[] fields = value.split(",");
                        return new UserBehavior(
                                fields[0], fields[1], fields[2], fields[3],
                                Long.parseLong(fields[4]) * 1000L
                        );
                    }
                }).filter(r -> r.type.equals("pv"))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<UserBehavior>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<UserBehavior>() {
                                    @Override
                                    public long extractTimestamp(UserBehavior element, long recordTimestamp) {
                                        return element.ts;
                                    }
                                })
                        // TODO 加随机数把数据打散避免数据倾斜。
                ).map(r -> Tuple2.of("pv" + new Random().nextInt(12), 1))
                .returns(Types.TUPLE(Types.STRING, Types.INT))
                .keyBy(r -> r.f0)
                .window(TumblingEventTimeWindows.of(Time.hours(1)))
                //TODO 打散的数据属于窗口的信息是不变的，根据窗口结束时间在做聚合
                .aggregate(new CountAgg(), new WindowResult())
                .keyBy(r -> r.windowEndTime)
                .process(new KeyedProcessFunction<Long, PageView, String>() {
                    private ListState<Long> accCount;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        accCount = getRuntimeContext().getListState(
                                new ListStateDescriptor<Long>(
                                        "acc",
                                        Types.LONG
                                )
                        );
                    }

                    @Override
                    public void processElement(PageView value, KeyedProcessFunction<Long, PageView, String>.Context ctx, Collector<String> out) throws Exception {
                        accCount.add(value.count);
                        // TODO 等到所有窗口数据收集齐就触发计算
                        ctx.timerService().registerEventTimeTimer(value.windowEndTime + 1L);
                    }

                    @Override
                    public void onTimer(long timestamp, KeyedProcessFunction<Long, PageView, String>.OnTimerContext ctx, Collector<String> out) throws Exception {
                        Long result = 0L;
                        Iterator<Long> iterator = accCount.get().iterator();
                        while (iterator.hasNext()){
                            Long next = iterator.next();
                            result+=next;
                        }
                        out.collect("窗口结束时间 " + new Timestamp((timestamp - 1L))+" pv："+result);
                    }
                })
                .print();


        env.execute();
    }

    public static class WindowResult extends ProcessWindowFunction<Long, PageView, String, TimeWindow> {

        @Override
        public void process(String s, ProcessWindowFunction<Long, PageView, String, TimeWindow>.Context context, Iterable<Long> elements, Collector<PageView> out) throws Exception {
            out.collect(new PageView(
                    "PV",
                    elements.iterator().next(),
                    context.window().getStart(),
                    context.window().getEnd()
            ));
        }
    }

    public static class CountAgg implements AggregateFunction<Tuple2<String, Integer>, Long, Long> {

        @Override
        public Long createAccumulator() {
            return 0L;
        }

        @Override
        public Long add(Tuple2<String, Integer> value, Long accumulator) {
            return accumulator + 1L;
        }

        @Override
        public Long getResult(Long accumulator) {
            return accumulator;
        }

        @Override
        public Long merge(Long a, Long b) {
            return a + b;
        }
    }
}
